class Observer {
  remove(typeName: string, callback: (...arg: any[]) => void) {
    this._listeners.get(typeName)?.delete(callback);
  }
  private static _instance?: Observer;
  constructor() {}
  static getInstance() {
    return this._instance || (this._instance = new Observer());
  }
  private _listeners = new Map<string, Set<(...arg: any[]) => void>>();

  on(typeName: string, listener: (...arg: any[]) => void) {
    if (!this._listeners.has(typeName)) {
      this._listeners.set(typeName, new Set());
    }
    const arr = this._listeners.get(typeName);

    arr?.add(listener);
  }

  emit(typeName: string, ...arg: any[]) {
    const arr = this._listeners.get(typeName);
    try {
      arr?.forEach((listener) => listener(...arg));
    } catch (error) {
      console.error(error);
    }
  }
}
